using System;
using System.Collections.Generic;
using System.Text;

namespace Chili.Opf3.Caching
{
	/// <summary>
	/// Put on persistent objects to specify the caching in a declarative way.
	/// </summary>
	/// <remarks>
	/// This attribute is put on persistent objects to specify the caching in a declarative way.
	/// </remarks>
	/// <example>
	///     The following example shows how to use attribute.
	///     <code lang="CS">
	/// [SimpleCache(Duration = 10000)]
	/// [Persistent("USER")]
	/// public class User
	/// {
	///     private string _name = null;
	///  
	///     [Field("NAME")]
	///     public string Name
	///     {
	///         get { return _name; }
	///         set { _name = value; }
	///     }
	///  
	///     // ... Other properties and fields.
	/// }
	/// </code>
	/// </example>
	[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
	public sealed class SimpleCacheAttribute : Attribute
	{
		private double _duration = -1;

		/// <summary>
		/// Gets and sets the expiration time (duration) in seconds. -1 is used for unlimited caching.
		/// </summary>
		public double Duration
		{
			get { return _duration; }
			set { _duration = value; }
		}

		/// <summary>
		/// Returns the time as time span.
		/// </summary>
		/// <returns></returns>
		internal TimeSpan GetDurationAsTimeSpan()
		{
			if (this.Duration <= -1)
				return TimeSpan.MaxValue;
			return TimeSpan.FromSeconds(this.Duration);
		}
	}
}
